package com.healthcare.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.healthcare.admin.pojo.User;
import com.healthcare.admin.query.UserQuery;
import com.healthcare.admin.service.UserService;
import com.healthcare.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 新华医院转诊联盟
 * @since 2020-04-12
 */
@Api(tags = "后台用户管理")
@RestController
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    public Result save(
            @ApiParam(name = "user", value = "新增用户对象", required = true)
            @RequestBody User user){
        user.setDeleted(false);
        userService.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "根据ID删除用户")
    @DeleteMapping("{id}")
    public Result removeById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable Integer id){
        userService.deleteUserInfoById(id);
        return Result.ok();
    }

    @ApiOperation(value = "分页后台用户列表")
    @GetMapping("{page}/{limit}")
    public Result pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    UserQuery userQuery
            ){

        Page<User> pageParam = new Page<>(page, limit);

        userService.pageQuery(pageParam,userQuery);
        List<User> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "根据ID查询用户信息")
    @GetMapping("{id}")
    public Result getById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable Integer id){

        User user = userService.getById(id);
        return Result.ok().data("item", user);
    }

    @ApiOperation(value = "根据ID修改用户")
    @PutMapping("{id}")
    public Result updateById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable Integer id,

            @ApiParam(name = "teacher", value = "用户对象", required = true)
            @RequestBody User user){

        user.setId(id);
        userService.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "验证手机号是否唯一")
    @GetMapping("mobile/{mobile}")
    public Result selectMobile(
            @ApiParam(name = "mobile", value = "用户手机号", required = true)
            @PathVariable String mobile){

        Integer judge =   userService.selectMobile(mobile);
        return Result.ok().data("judge",judge);
    }

    @ApiOperation(value = "验证管理员是否唯一")
    @GetMapping("Administrator/{identity}")
    public Result selectAdministrator(
            @ApiParam(name = "identity", value = "用户状态", required = true)
            @PathVariable Integer identity){
        Integer judge =   userService.selectAdministrator(identity);
        return Result.ok().data("judge",judge);
    }

    @ApiOperation(value = "Excel批量添加用户")
    @PostMapping("import")
    public Result batchImport(
            @ApiParam(name = "file", value = "Excel文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {

        userService.batchImport(file);

        return Result.ok();
    }

}

